package com.ebei.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ebei.admin.mapper.SysOrgRoleMapper;
import com.ebei.admin.model.dto.OrgRoleDTO;
import com.ebei.admin.model.dto.OrgRoleUserDTO;
import com.ebei.admin.model.entity.SysOrgRole;
import com.ebei.admin.model.entity.SysOrgRoleUser;
import com.ebei.admin.service.PositionService;
import com.ebei.admin.service.SysOrgRoleUserService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("positionService")
public class PositionServiceImpl extends ServiceImpl<SysOrgRoleMapper,SysOrgRole> implements PositionService   {
    @Autowired
    private SysOrgRoleMapper sysOrgRoleMapper;

    @Autowired
    private SysOrgRoleUserService sysOrgRoleUserService;

    @Override
    public List<OrgRoleDTO> selectPositionsByOrgId(Integer orgId) {
        return sysOrgRoleMapper.selectPositionsByOrgId(orgId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Boolean delPosition(Integer positionId) {
        //删除岗位关联
        SysOrgRoleUser condition = new SysOrgRoleUser();
        condition.setPositionId(positionId);
        sysOrgRoleUserService.delete(new EntityWrapper<>(condition));

        // 删除岗位
        SysOrgRole sysOrgRole = this.selectById(positionId);
        sysOrgRole.setDelFlag(CommonConstant.STATUS_DEL);
        this.updateById(sysOrgRole);

        return this.updateById(sysOrgRole);
    }

    @Override
    public Page selectUsersByPositionId(Query<OrgRoleUserDTO> objectQuery) {
        objectQuery.setRecords(sysOrgRoleMapper.selectUsersByPositionId(objectQuery,objectQuery.getCondition()));
        return objectQuery;
    }
}
